//
//  TestWindow.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2023/7/11.
//  Copyright © 2023 moon. All rights reserved.
//

#import <MKUI/MKUI.h>

#ifdef DEBUG
@interface TestWindow : UIView
@end
@interface TestWindow ()
@property(nonatomic,strong) MKUIFlowLayoutButton *debugButton;
@property(nonatomic,assign) CGPoint beginPoint;
@property(nonatomic,assign) CGRect beginFrame;
@end

@implementation TestWindow
+ (void)load{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        TestWindow *window = [[TestWindow alloc] init];
        [window sizeToFit];
        CGRect f1 = window.frame;
        CGRect bounds = UIApplication.sharedApplication.keyWindow.bounds;
        if([NSUserDefaults.standardUserDefaults objectForKey:@"DebugButton_X"]){
            f1.origin.x = [NSUserDefaults.standardUserDefaults floatForKey:@"DebugButton_X"];
            f1.origin.y = [NSUserDefaults.standardUserDefaults floatForKey:@"DebugButton_Y"];
        }else{
            f1.origin.x = bounds.size.width-f1.size.width;
            f1.origin.y = bounds.size.height-f1.size.height-60;
        }
        window.frame = f1;
        window.hidden = NO;
        window.userInteractionEnabled = YES;
        [UIApplication.sharedApplication.keyWindow addSubview:window];
    });
}
- (id)init{
    if(self=[super init]){
        self.backgroundColor = UIColor.clearColor;
        self.debugButton = [[MKUIFlowLayoutButton alloc] init];
        [self.debugButton setTitle:@"Dard" forState:(UIControlStateNormal)];
        [self.debugButton setTitle:@"Light" forState:(UIControlStateSelected)];
        [self.debugButton addTarget:self action:@selector(__ondebug) forControlEvents:(UIControlEventTouchUpInside)];
        self.debugButton.backgroundColor = [UIColor lightGrayColor];
        [self.debugButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        self.debugButton.clipsToBounds = YES;
        [self addSubview:self.debugButton];
        UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(__ontap:)];
        [self addGestureRecognizer:gesture];
    }
    return self;
}
- (void)__ontap:(UITapGestureRecognizer *)gesture{
    CGPoint point = [gesture locationInView:self.superview];
    if(gesture.state==UIGestureRecognizerStateBegan){
        self.beginPoint = point;
        self.beginFrame = self.frame;
        return;
    }
    CGPoint dis = CGPointZero;
    dis.x = point.x-self.beginPoint.x;
    dis.y = point.y-self.beginPoint.y;
    CGRect f1 = self.beginFrame;
    f1.origin.x = self.beginFrame.origin.x+dis.x;
    f1.origin.y = self.beginFrame.origin.y+dis.y;
    self.frame = f1;
    if(gesture.state==UIGestureRecognizerStateEnded){
        [NSUserDefaults.standardUserDefaults setFloat:f1.origin.x forKey:@"DebugButton_X"];
        [NSUserDefaults.standardUserDefaults setFloat:f1.origin.y forKey:@"DebugButton_Y"];
    }
}
- (void)__ondebug{
    self.debugButton.selected = !self.debugButton.selected;
    if (@available(iOS 13.0, *)) {
        UIUserInterfaceStyle style = UIApplication.sharedApplication.keyWindow.overrideUserInterfaceStyle;
        switch (style) {
            case UIUserInterfaceStyleLight:
                style = UIUserInterfaceStyleDark;
                break;
            case UIUserInterfaceStyleDark:
                style = UIUserInterfaceStyleLight;
                break;
            case UIUserInterfaceStyleUnspecified:
            default:
                if([UITraitCollection mk_isDarkStyle]){
                    style = UIUserInterfaceStyleLight;
                }else{
                    style = UIUserInterfaceStyleDark;
                }
                break;
        }
        UIApplication.sharedApplication.keyWindow.overrideUserInterfaceStyle = style;
        UIWindow.mkui_sharedAlertWindow.overrideUserInterfaceStyle = style;
    }
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    CGRect f1 = bounds;
    f1.size.width = f1.size.height = 50;
    MKCGRectAlignMaxYToRect(&f1, bounds);
    self.debugButton.layer.cornerRadius = MIN(f1.size.height,f1.size.width)*0.5;
    self.debugButton.frame = f1;
}
- (CGSize)sizeThatFits:(CGSize)size{
    return CGSizeMake(50, 50);
}
@end
#endif
